package org.restorator.service.impl;

import org.restorator.dao.base.TableDao;
import org.restorator.dao.implement.TableDaoMock;
import org.restorator.dao.tx.TransactionManager;
import org.restorator.dao.tx.TransactionManagerJdbc;
import org.restorator.dao.tx.TxManagerJdbc;
import org.restorator.entity.AnonymTable;
import org.restorator.entity.Table;
import org.restorator.entity.dto.TableDto;
import org.restorator.exception.ServiceException;
import org.restorator.exception.TransactionException;
import org.restorator.service.DtoAssebler;
import org.restorator.service.TableService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import org.restorator.dao.implement.TableDaoImpl;
import org.restorator.utill.GeneralUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Snisar Bogdan.
 */


public class TableServiceImpl implements TableService, DtoAssebler<Table, TableDto> {

    private static Logger logger = LoggerFactory.getLogger(GeneralUtil.getClassName());
    private TableDao tableDao = new TableDaoImpl();
    private TransactionManager tx = TxManagerJdbc.INSTANCE;

    @Override
    public List<Table> getAllTables() throws ServiceException {
        return tx.doInTran(new Callable<List<Table>>() {
            @Override
            public List<Table> call() throws Exception {
                return tableDao.getAll();
            }
        });
    }

    @Override
    public TableDto toDto(Table actual) {
        TableDto dto = new TableDto();
        if(actual.getOrder() != null){
            dto.setOrderId(actual.getOrder().getOrderId());
        }

        
        dto.setTableID(actual.getTableID());
        dto.setStatus(actual.getStatus());
        dto.setX(actual.getX());
        dto.setY(actual.getY());
        logger.debug("FromDTO TableDto = {}", dto);
        return dto;
    }

    @Override
    public ArrayList<TableDto> toDto(Collection<Table> actual) {
        logger.debug("FromDTO actual = {}", actual);
        ArrayList<TableDto> list = new ArrayList();
        for(Table t : actual) {
            list.add(toDto(t));
        }
        logger.debug("FromDTO list = {}", actual);
        return list;
    }

    @Override
    public Table fromDto(TableDto dto) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Collection<Table> fromDto(Collection<TableDto> dtos) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Table get(final Integer primaryKey) throws ServiceException {
        return tx.doInTran(new Callable<Table>() {
            @Override
            public Table call() throws Exception {
                return tableDao.get(primaryKey);
            }
        });
    }

    @Override
    public void update(final Table entity) throws ServiceException {
        tx.doInTran(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                tableDao.update(entity);
                return null;  //To change body of implemented methods use File | Settings | File Templates.
            }
        });
    }

    @Override
    public Integer save(final Table entity, Object... arg) throws ServiceException {
        return tx.doInTran(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return tableDao.save(entity);
            }
        });
    }

    @Override
    public void delete(final Table entity) throws ServiceException {
        tx.doInTran(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                tableDao.delete(entity);
                return null;
            }
        });
    }

    @Override
    public List<Table> getAll() throws ServiceException {
        return tx.doInTran(new Callable<List<Table>>() {
            @Override
            public List<Table> call() throws Exception {
                return tableDao.getAll();
            }
        });
    }
}
